package com.xiaohuihui.springbootsecurity.handler;

import com.alibaba.fastjson.JSON;
import com.xiaohuihui.springbootsecurity.domain.ApiResult;
import com.xiaohuihui.springbootsecurity.utils.WebUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) {
        ApiResult apiResult =ApiResult.failed("授权失败");
        String json = JSON.toJSONString(apiResult);
        //处理异常
        WebUtils.renderString(response,json);
    }
}